<?php

namespace App;

use Carbon\Carbon;
use Eloquent;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;

/**
 * App\User
 *
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $username 管理员名称
 * @property string $password 密码
 * @property string $name 姓名
 * @property string $mobile 手机号
 * @property string $phone 电话
 * @property string $email 邮箱
 * @property string $sex 性别
 * @property string $duty 职务
 * @property int $power 权限
 * @property int $depart_id 部门id
 * @property int $company_id 公司id
 * @property int $role_id 角色id
 * @property int $on_job 在职
 * @property int $phone_type 手机类型
 * @property \Carbon\Carbon $created_at 创建时间戳
 * @property \Carbon\Carbon $updated_at 修改时间戳
 * @property int $start_at 参加工作时间
 * @property int $unit_start_at 本单位工作时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCompanyId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereDepartId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereDuty($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereOnJob($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePhoneType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePower($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRoleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereStartAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUnitStartAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUsername($value)
 * @property-read \App\Company $company
 * @property-read mixed $on_job_label
 * @property-read mixed $power_label
 * @property-read mixed $type_label
 * @property-read \App\Depart|null $depart
 * @property-read \App\Role|null $role
 * @property string|null $remember_token
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Role[] $roles
 */
class User extends Authenticatable
{
    use Notifiable;
    use EntrustUserTrait;

    //权限类型
    const POWER_ADMIN = 0;
    const POWER_USER = 1;
    //登录终端
    const TYPE_LEAVE = -1;
    const TYPE_PC = 0;
    const TYPE_IOS = 1;
    const TYPE_ANDROID = 2;

    const ON_JOB = 1;
    const ON_JOB_OTHER = 2;
    const NOT_ON_JOB = 0;
    const RETIRE = 3;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'on_job' => [
                self::ON_JOB => '在职(正式)',
                self::ON_JOB_OTHER => '在职(外聘)',
                self::RETIRE => '退休',
                self::NOT_ON_JOB => '离职',
            ],
            'power' => [
                self::POWER_ADMIN => '管理员',
                self::POWER_USER => '用户',
            ],
            'type' => [
                self::TYPE_LEAVE => '离线',
                self::TYPE_PC => 'pc',
                self::TYPE_IOS => 'ios',
                self::TYPE_ANDROID => '安卓',
            ]
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) && isset($dropDownList[$column][$value]) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }

    //可以修改权限
    public function canEditPower()
    {
        return (!$this->id )||($this->power == self::POWER_USER);
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    public function getOnJobLabelAttribute($value)
    {
        return self::dropdown('on_job',$this->on_job);
    }

    public function getPowerLabelAttribute($value)
    {
        return self::dropdown('power',$this->power);
    }

    public function getTypeLabelAttribute($value)
    {
        return self::dropdown('type',$this->on_job);
    }

    public function getStartAtAttribute($value)
    {
        return Carbon::createFromTimestamp($value);
    }

    public function setStartAtAttribute($value)
    {
        $this->attributes['start_at'] = strtotime($value);
    }

    public function getUnitStartAtAttribute($value)
    {
        return Carbon::createFromTimestamp($value);
    }

    public function setUnitStartAtAttribute($value)
    {
        $this->attributes['unit_start_at'] = strtotime($value);
    }

    public function formStartAtAttribute($value)
    {
        return Carbon::createFromTimestamp($value)->format('Y-m-d');
    }

    public function formUnitStartAtAttribute($value)
    {
        return Carbon::createFromTimestamp($value)->format('Y-m-d');
    }

    /**
     * @param \DateTime|int $value
     * @return false|int
     * @author dividez
     */
    public function fromDateTime($value)
    {
        return strtotime(parent::fromDateTime($value));
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function depart(){
        return $this->belongsTo(Depart::class);
    }

    public function role(){
        return $this->belongsTo(Role::class);
    }
}
